package com.tsoj.utils;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.concurrent.TimeUnit;

/**
 * redis工具类
 */
@Component
public class RedisUtils implements Serializable {

    @Resource
    private RedisTemplate<String,Object> redisTemplate;
    public void set(String key, Object value){
        redisTemplate.opsForValue().set(key, value);
    }
    public void set(String key, Object value, long expire, TimeUnit unit){
        //带过期时间存储
        redisTemplate.opsForValue().set(key, value, expire, unit);
    }
    public Object get(String key){
        return redisTemplate.opsForValue().get(key);
    }
    public void delete(String key){
        redisTemplate.delete(key);
    }
    //判断这个key是否存在
    public boolean hasKey(String key){
        return redisTemplate.hasKey(key);
    }
}
